// reduceRequest(回调).then(res=>{})

export function reduceRequest(promiseProducer, params, interval, attemptTimes) {
  let promise = null;
  //1. interval 间隔时间 >=300  500
  interval = typeof interval === 'number' && interval >= 300 ? interval : 500;
  attemptTimes = typeof attemptTimes === 'number' && attemptTimes >= 1 ? attemptTimes : 10000;

  // 2.返回一个promise
  return new Promise((resolve, reject) => {
    // 函数 : 处理次数
    // 无限次调用 只要次数还在
    let executePromise = timer => {
      // 成功 promise
      // 失败 new Error ()
      // 次数
      //1. 次数用完了 错误
      //3. 处理成功
      if (attemptTimes < 1) {
        window.clearInterval(timer);
        reject(new Error('promise not until timeout'));
        return void 0;// undefined
      }
      //2. 次数--
      attemptTimes -= 1;
      return promiseProducer(params).then(res => {
        window.clearInterval(timer);
        resolve(res);
      }).catch(e => {
        throw new Error(e)
      })
    };

    //timer 定时器  attemptTimes
    let timer = window.setInterval(() => {
      promise = executePromise(timer)
    }, interval);
    promise = executePromise(timer);
  })
}

export function reduceRequests(promiseProduces = [], params, interval, attemptTimes) {
  let promise = null;
  //1. interval 间隔时间 >=300  500
  interval = typeof interval === 'number' && interval >= 300 ? interval : 500;
  attemptTimes = typeof attemptTimes === 'number' && attemptTimes >= 1 ? attemptTimes : 10000;

  // 2.返回一个promise
  return new Promise((resolve, reject) => {
    // 函数 : 处理次数
    // 无限次调用 只要次数还在
    let executePromise = timer => {
      // 成功 promise
      // 失败 new Error ()
      // 次数
      //1. 次数用完了 错误
      //3. 处理成功
      if (attemptTimes < 1) {
        window.clearInterval(timer);
        reject(new Error('promise not until timeout'));
        return void 0;// undefined
      }
      //2. 次数--
      attemptTimes -= 1;
      // [promiseProduces,promiseProduces] // 每次请求 都会打印成功或者失败
      // 调用链  第一个请求失败了 就不会请求第二个 会一直请求第一个
      // return promiseProducer(params).then(res => {
      //   window.clearInterval(timer);
      //   resolve(res);
      // }).catch(e => {
      //   throw new Error(e)
      // })
    };

    //timer 定时器  attemptTimes
    let timer = window.setInterval(() => {
      promise = executePromise(timer)
    }, interval);
    promise = executePromise(timer);
  })
}

export function reduceRequests1(promiseProduces = [], params = [], interval, attemptTimes) {
  let promise = null;
  let times = attemptTimes;
  //1. interval 间隔时间 >=300  500
  interval = typeof interval === 'number' && interval >= 300 ? interval : 500;
  attemptTimes = typeof attemptTimes === 'number' && attemptTimes >= 1 ? attemptTimes : 10000;

  // 2.返回一个promise
  return new Promise(async (resolve, reject) => {
    times = attemptTimes;
    promiseProduces = Array.isArray(promiseProduces) ? promiseProduces : [];
    let promiseRes = [];
    for (let i = 0; i < promiseProduces.length; i++) {
      let executePromise = async timer => {
        if (times < 1) {
          window.clearInterval(timer);
          promiseRes.push('promise not until timeout');
          reject(new Error('promise not until timeout'));
          return void 0;// undefined
        }
        //2. 次数--
        times -= 1;
        let res = await promiseProduces[i](params[i]).catch(e => {
          throw new Error(e);
        });
        promiseRes.push(res);
        window.clearInterval(timer);
      };
      //timer 定时器  attemptTimes
      let timer = window.setInterval(async () => {
        promise = await executePromise(timer)
      }, interval);
        promise = await executePromise(timer);
    }
    resolve(promiseRes);
  })
}
